<template>
	<view class="safe">
		<view class="container" style="height: 1px;background: #f3f3f3;"></view>
		<view class="container flex-xy">
			<view class="w90">
				<u-form :model="form" ref="uForm" label-width="200rpx">
				<!--  -->
				<u-form-item v-if="JudgePassword=='false'" class="mt10 mb20" label="当前密码" prop="passwordOld"><u-input type="number" v-model="form.passwordOld" placeholder="请输入当前密码" :clearable="false" /></u-form-item>
					<u-form-item class="mt10 mb20" :label="label1" prop="password"><u-input type="number" v-model="form.password" :placeholder="placeholder1" :clearable="false" /></u-form-item>
					<u-form-item class="mt10 mb20" :label="label2" prop="passwordConfirm">
						<u-input type="number" v-model="form.passwordConfirm" :placeholder="placeholder2" :clearable="false" />
					</u-form-item>
				</u-form>
				<u-button @click="submit" type="primary" :ripple="true">提交</u-button>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			label1: '输入支付密码',
			label2: '确认支付密码',
			placeholder1: '请输入支付密码',
			placeholder2: '请确认支付密码',
			JudgePassword: null, //是否有密码
			// true无密码
			// false有密码
			form: {
				passwordOld:'',//初始密码
				password: '',//赋值密码
				passwordConfirm: ''//确认密码
			},
			rules: {
				// passwordOld:[
				// 	{
				// 		required: true,
				// 		message: '请输入初始密码',
				// 		trigger: ['blur', 'change']
				// 	},
				// 	// 6-8个字符之间的判断
				// 	{
				// 		min: 6,
				// 		max: 6,
				// 		message: '长度是六位'
				// 	}
				// ],
				password: [
					{
						required: true,
						message: '请输入支付密码',
						trigger: ['blur', 'change']
					},
					// 6-8个字符之间的判断
					{
						min: 6,
						max: 6,
						message: '长度是六位'
					}
				],
				passwordConfirm: [
					{
						required: true,
						message: '请确认支付密码',
						trigger: ['blur', 'change']
					},
					// 6-8个字符之间的判断
					{
						min: 6,
						max: 6,
						message: '长度是六位'
					}
				]
			}
		};
	},
	onLoad() {
		// GET/mobileUser/password/JudgePassword/{userId}是否有密码
		// * true    无密码
		//  * false   有密码
		var postObj = { url: `/mobileUser/password/JudgePassword/${uni.getStorageSync('y-userId')}`, data: {} };
		this.getRequest(postObj)
			.then(res => {
				// console.log('res查询是否有密码', res);
				if (res.data.code == 200) {
					this.JudgePassword = res.data.msg;

					// false有密码  修改密码文案
					if (res.data.msg == 'false') {
						this.label1 = '输入修改密码';
						this.label2 = '确认修改密码';
						this.placeholder1 = '请输入修改密码';
						this.placeholder2 = '请确认修改密码';
					}
				}
			})
			.catch(err => {
				// console.log('err查询是否有密码', err);
			});
	},
	methods: {
		submit() {
			this.$refs.uForm.validate(valid => {
				if (valid) {
					if (this.form.password == this.form.passwordConfirm) {
						// uni.showToast({
						// 	title: '验证通过',
						// 	icon: 'none'
						// });
						// 	JudgePassword:null,//是否有密码

						let JudgePassword = this.JudgePassword;
						// true无密码  调用新增
						if (JudgePassword == 'true') {
							// POST/mobileUser/password/addPassword新增密码
							// {
							//   "password": "123456",
							//   "userId": "2"
							// }

							let getObj = {
								url: '/mobileUser/password/addPassword',
								data: {
									password: this.form.password,
									userId: uni.getStorageSync('y-userId')
								}
							};
							this.postRequest(getObj)
								.then(res => {
									if (res.data.code == 200) {
										uni.showToast({
											title: res.data.msg,
											mask: true,
											icon: 'none',
											duration: 500,
											success() {
												setTimeout(() => {
													uni.navigateBack({
														delta: 1
													});
												}, 500);
											}
										});
									} else {
										uni.showToast({
											title: res.data.msg,
											mask: true,
											icon: 'none',
											duration: 500
										});
									}
								})
								.catch(err => {});
						}
						// false有密码  调用修改
						if (JudgePassword == 'false') {
							// POST/mobileUser/password/modifyPassword修改密码
							// {
							//   "password": "123456",
							//   "newPassword":"qaz123",
							//   "userId": "2"
							// }

							let getObj = {
								url: '/mobileUser/password/modifyPassword',
								data: {
									password: this.form.passwordOld,
									newPassword: this.form.password,
									userId: uni.getStorageSync('y-userId')
								}
							};
							this.postRequest(getObj)
								.then(res => {
									if (res.data.code == 200) {
										uni.showToast({
											title: res.data.msg,
											mask: true,
											icon: 'none',
											duration: 500,
											success() {
												setTimeout(() => {
													uni.navigateBack({
														delta: 1
													});
												}, 500);
											}
										});
									} else {
										uni.showToast({
											title: res.data.msg,
											mask: true,
											icon: 'none',
											duration: 500
										});
									}
								})
								.catch(err => {});
						}
					} else {
						uni.showToast({
							title: '密码输入不一致！',
							icon: 'none'
						});
					}
				} else {
					// console.log('验证失败');
				}
			});
		}
	},
	// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	}
};
</script>

<style lang="less" scoped>
page {
	background: #ffffff;
}
</style>
